Skip to content

fix(event_handler): send cookie Expires in GMT with English day and month names - #8876

Open
cleissonom wants to merge 1 commit into
aws-powertools:developfrom
cleissonom:fix/cookie-expires-utc
Open

cleissonom wants to merge 1 commit into
aws-powertools:developfrom
cleissonom:fix/cookie-expires-utc

Conversation

@cleissonom

@cleissonom cleissonom commented Nov 19, 2026 •

Copy link
Copy Markdown

Issue number:
closes #8671

Summary

Changes

Cookie formats expiry dates with strftime and a fixed GMT suffix. For a non-UTC datetime, that changes the intended expiry time by its UTC offset. The day and month names also depend on LC_TIME, so a non-English locale can produce an invalid cookie date.

_format_date now converts the value to UTC and uses email.utils.format_datetime(..., usegmt=True). This fixes both problems discussed in #8671. The import stays inside the function so its cost is deferred until an expiry value is formatted.

Naive datetimes still mean UTC, and date values still mean midnight GMT. UTC conversions outside years 1 to 9999 are clamped to the nearest representable boundary to avoid introducing OverflowError. The constructor docstring explains the timezone handling.

User experience

Expiry input Before After
datetime(2026, 1, 1, 12, tzinfo=timezone(timedelta(hours=2))) Expires=Thu, 01 Jan 2026 12:00:00 GMT Expires=Thu, 01 Jan 2026 10:00:00 GMT
datetime(2026, 10, 30, 7, 28) with LC_TIME=de_DE.UTF-8 Expires=Fr., 30 Okt. 2026 07:28:00 GMT Expires=Fri, 30 Oct 2026 07:28:00 GMT

The cookie payload shape is unchanged. The date text changes without a deprecation warning under ADR 0006: the previous output contradicted the RFC 6265 cookie date format, and the maintainer confirmed both fixes in the issue.

Verification

I used an AI coding assistant.

I ran uv run --locked pytest tests/unit/test_cookie_class.py tests/functional/event_handler/required_dependencies/test_api_gateway.py -q -k cookie on this commit, with cookies.py taken from develop (4 failed, 16 passed) and with the change (20 passed), and read both outputs.

The added tests cover timezone conversion, locale-independent formatting, date values, range boundaries, and cookie output through APIGatewayRestResolver and APIGatewayHttpResolver. The locale unit test uses a datetime subclass to emulate German strftime output, following #8707, so it does not depend on installed locales.

Other recorded checks on macOS arm64, with locked dependencies and base a2eb4d59:

  • All 16 cookie unit tests pass. The timezone, locale and resolver regressions fail on the base. The boundary test passes on the base but catches OverflowError if conversion is added without the clamp.
  • Cookie, event-handler and header-serializer tests pass on Python 3.10.22, 3.11.17, 3.12.15, 3.13.15 and 3.14.6; mypy and ty report no issues. The only skip is the unconditional test_channel_path_normalization skip.
  • make pr on Python 3.14.6: 4,241 passed, 73 skipped and 0 failed; 10 performance tests passed. Mypy and bandit pass, cookies.py remains at xenon rank A, and generated skill references are unchanged.
  • Comparison with 3.41.0 preserves the tested naive, UTC, date, microsecond and boundary outputs. Non-UTC values, including year rollover, Europe/Berlin and America/Sao_Paulo, get the corrected GMT time. Real de_DE, fr_FR and ja_JP locale checks produce English names.

These checks were local. The change has not been run against a real API Gateway, ALB or Function URL, and no AWS resources were used.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@cleissonom
cleissonom requested a review from a team as a code owner November 19, 2026 13:53
@boring-cyborg

boring-cyborg Bot commented Nov 19, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

…onth names

A timezone-aware expires was formatted as its local wall-clock time with a
GMT suffix, so the cookie expired early or late by the UTC offset. strftime's
%a and %b also followed the process LC_TIME locale, producing names browsers
cannot parse.

Convert aware datetimes to UTC and format with email.utils.format_datetime.
Naive datetimes keep being read as UTC and a date keeps meaning midnight GMT.
An instant outside years 1 to 9999 is clamped instead of raising OverflowError.
@cleissonom
cleissonom force-pushed the fix/cookie-expires-utc branch from 805ef99 to 30615f4 Compare November 19, 2026 15:12
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commons size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Cookie Expires labels a non-UTC aware datetime as GMT without converting it

2 participants